<?php
require_once "./header.php";
$conn = connect_to_db();

//此处开始设置监控访问日志需要的监控代码----------------------------------------
$PKey = 'wanmi';//项目key值
$splitFlag = false;//是否分表 true分表 false没有分表
$pvuvLogObj = new pvuvLog($PKey, $splitFlag);
$pvuvLogObj->visitWeixinID = $openid;//访问当前页面的用户
$pvuvLogObj->kmResult = "";//页面中需要保存的数据;
$url = $_SERVER['PHP_SELF'];
$pageName = substr($url, strrpos($url, '/') + 1);
$pvuvLogObj->urlID = $PKey . "_istore_next_$pageName";//页面ID 项目key_页面名称


//CONM是wanmi自身数据库通过bs_member表查询
$conm = mysqli_connect($sqlHost , $sqlUser , $sqlPsd,'marlboro_wanmi');
$query = "SELECT act_code,verify,nickname,headimgurl,integral FROM bs_member WHERE openid = '$openid'";
$result = mysqli_query($conm,$query);

$confirmRule = 0;

$header = '';
$is_member = '1';
$member['act_code'] = 'subscribe';

if(/*is_resource($result) || mysqli_num_rows($result) == 1*/true){
	$member = mysqli_fetch_assoc($result);
	if($member['act_code'] != 'unsubscribe'){
		if($member['verify'] == '1'||$member['verify'] == '0'){
			$is_member = '1';
//判断是否答应条款
			$query = "SELECT openid FROM bs_confirm_rule WHERE openid = '$openid' AND activity_name = '$wanmiProjectEn'";
			$result = mysqli_query($conm,$query);
			if(mysqli_num_rows($result) != 0){

				$confirmRule = 1;
			}else{
//				$header = "http://wanmiclub.lovemojito.com/wanmi_user_rule/confirmRule.php?callback="."http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			}

		}else{
			$is_member = '0';
			$header = "http://wanmiclub.lovemojito.com/wx/index.php?s=/Home/member/f1bind";
		}
	}else{
		$header='error.html';
	}
}else{
//    echo $query."</br>";
	$header='error.html';
}

//是否兑换过
$isCost = 0;
$query = "SELECT * FROM bs_integral_store_next_user WHERE openid = '$openid'";
$result = mysqli_query($conm,$query);
if(mysqli_num_rows($result) != 0){
	$isCost = 1;
	$prizeId = mysqli_fetch_assoc($result)['prize'];
    $member['integral'] = 0;
}

if($header!=''){
	header('Location:'.$header);
	echo $header;
	$pvuvLogObj->kmResult = 'Location:'.$header;
	$pvuvLogObj->saveLog();
	exit();
}

$unixTime = time();
$ajaxKey = 'dkn23m@63!ds';
$token = md5($openid.$unixTime.$ajaxKey);


?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <script src="js/responsive.js"></script>
    <title>万迷聚乐部</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        body{
            background: url(img/background.png)no-repeat;
            background-size: 100%;
        }
        .top-title{
            background: url(img/top-background.png) no-repeat center;
            background-size: 34%;
            height: 19vw;
        }
        .top-title img{
            width: 15vw;
        }
        .top-title img:first-child{
            margin: 7.3vw 0 0 34vw;
        }
        .top-title img:last-child{
            margin-bottom: 9px;
        }
        .rule{
            width: 14vw;
            position: relative;
            float: right;
            top: -46px;
            right: 9px;
            cursor: pointer;
        }
        .user-Info{
            background: url("img/info-background.png") no-repeat center;
            background-size: 85%;
            height: 25vw;
            position: relative;
        }
        .user-photo{
            position: absolute;
            top: 5.5vw;
            left: 15vw;
            width: 12vw;
        }
        .user{
            position: absolute;
            left: 30vw;
            top: 8vw;
            /*font-size: 0.8rem;*/
            font-weight: 500;
        }
        .integral{
            position: absolute;
            font-family: Arial sans-serif;
            font-size: 1.7rem;
            font-weight: 500;
            color: #D7182A;
            right: 18vw;
            top: 7vw;
            vertical-align: -5px;
        }
        .my-into{
            width: 19vw;
            position: relative;
            float: right;
        }
        .first-head {
            width: 95%;
            margin-top: 10px;
        }
        .first-in{
            top: -34.2vw;
            right: 30vw;
        }
        .second-back{
            width: 91%;
            float: right;
            margin-top: -12vw;
            height: 33vw;
        }
        .second-in{
            top: -34.3vw;
            right: 13.5vw;
            height: 10.8vw;
        }
        .third-title{
            width: 92%;
            height: 13vw;
            float: left;
            margin-top: -12vw;
        }
        .third-in{
            top: -37.3vw;
            right: 18vw;
            height: 10.8vw;
        }
        .third-prize{
            width: 85%;
            margin: -3vw 0 0 5vw;
        }
        .fourth-back{
            width: 93%;
            float: right;
            margin-top: -14vw;
            height: 32vw;
        }
        .fourth-in{
            margin-top: -33.4vw;
            right: 10vw;
            height: 10.8vw;
        }
        .rule-content{
            display: none;
            background-color: white;
            width: 96%;
            text-align: center;
            margin: 10px 0;
            border-radius: 25px;
            min-height: 100%;
            overflow-y: scroll;
        }
        .rule-content img:first-child{
            margin: 20px 0;
            width: 90%;
        }
        .rule-content #agree-return{
            width: 29%;
            margin:	5px 0 20px ;
        }
        .rule-content input{
            vertical-align: -2px;
            margin-right: 5px;
        }
        .rule-content span{
            font-size: 1rem;
            font-weight: 300;
        }
        .back{
            position: absolute;
            float: right;
            top: 4vw;
            right: 4vw;
        }
        .succeed{
            width: 100%;
            display: none;
            text-align: center;
        }
        .succeed .confirm-btn{
            width: 20vw;
            position: relative;
            top: -14vw;
        }
        .into{
            cursor: pointer;
            z-index: 999;
        }
        #ExchComplete{
            cursor: pointer;
        }
    </style>
</head>

<body>
<section>
    <div class="top-title">
        <img src="img/integral.png" />
        <img src="img/thank.png" />
    </div>
    <img id="rule" src="img/rule.png?v=1" class="rule exchange-rule">
</section>
<section class="user-Info">
    <img class="user-photo" src="<?php echo $member['headimgurl']; ?>">
    <span class="user"><?php echo $member['nickname']; ?></span>
    <span id="integral" class="integral"><?php echo $member['integral']; ?>积分</span>
    <span></span>
</section>
<!--1-500-->
<section class="first">
    <img itemid="1" src="img/first.png" class="into first-head" >
    <img itemid="1" src="img/into.png" class="into my-into first-in">
</section>
<!--500-800-->
<section class="">
    <img itemid="2" src="img/second.png" class="into second-back">
    <img itemid="2" src="img/into.png" class="into my-into second-in">
</section>
<!--800-5000-->
<section>
    <img itemid="3" src="img/third-title.png" class="into third-title">
    <img itemid="3" src="img/third-prize.png" class="into third-prize">
    <img itemid="3" src="img/into.png" class="into my-into third-in">
</section>
<!--5000以上-->
<section>
    <img itemid="4" src="img/fourth.png" class="into fourth-back">
    <img itemid="4" src="img/into.png" class="into my-into fourth-in">
</section>
<!--规则弹窗-->
<div id="rule-bPopup" class="rule-content">
    <img src="img/ruleContent.png" ><br/>
    <img src="img/close.png" class="back b-close">
	<?php if(!$confirmRule){ ?>
        <input type="checkbox"  checked="checked" id="checkbox"/><span class="ihave">我已阅读并同意该协议</span><br>
        <a id="agree-return"><img  src="img/agree-rule.png" ></a>
	<?php } ?>
</div>
<!--10元话费-->
<div id="10yuanhuafei" class="succeed">
    <img src="img/success-10yuanhuafei.png" width="90%"  >
</div>
<!--30元话费-->
<div id="30yuanhuafei" class="succeed">
    <img src="img/success-30yuanhuafei.png" width="90%"  >
</div>
<!--10元话费-->
<div id="jbl" class="succeed">
    <img src="img/success-jbl.png" width="90%"  >
</div>
<!--lamy-->
<div id="lamy" class="succeed">
    <img src="img/success-lamy.png" width="90%"  >
</div>
<!--mouse-->
<div id="mouse" class="succeed">
    <img src="img/success-mouse.png" width="90%"  >
</div>
<!--qqy音乐-->
<div id="qqyinyue" class="succeed">
    <img src="img/success-qqyinyue.png" width="90%"  >
</div>
<!--星巴克-->
<div id="xingbake" class="succeed">
    <img src="img/success-xingbake.png" width="90%"  >
</div>
<!--zippo-->
<div id="zippo" class="succeed">
    <img src="img/success-zippo.png" width="90%"  >
</div>
<!--积分不足弹框-->
<div id="integralNotEnough" class="succeed">
    <img src="img/exchangePage/integral-not-enough.png" width="80%"  >
    <img  id="ExchComplete" src="img/exchangePage/ExchSuccessSure.png" class="b-close confirm-btn" >
</div>
<!--积分不对应-->
<div id="integralNotRight" class="succeed">
    <img src="img/exchangePage/integral-not-right.png" width="80%"  >
    <img id="ExchComplete" src="img/exchangePage/ExchSuccessSure.png" class="b-close confirm-btn">
</div>
</body>
</html>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js?v=1?"></script>
<script src="js/jquery.bpopup.min.js"></script>
<script>

    var confirmRule = '<?php  echo $confirmRule;  ?>';
    var integral = '<?php echo $member['integral']; ?>';
    var openid = '<?php echo $openid; ?>';


	<?php
	if($isCost){
	if($prizeId == '1'){ //10元话费
	?>
    buttonLog('window-have-10yuanhuafei');
    $('#10yuanhuafei').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	if($prizeId == '2'){ //qqyinyue
	?>
    buttonLog('window-have-qqyinyue');

    $('#qqyinyue').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	if($prizeId == '3'){ //xingbake
	?>
    buttonLog('window-have-xingbake');

    $('#xingbake').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	if($prizeId == '4'){ //30yuan
	?>
    buttonLog('window-have-30yuanhuafei');

    $('#30yuanhuafei').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	if($prizeId == '5'){ //lamy
	?>
    buttonLog('window-have-30yuanhuafei');
    $('#lamy').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	if($prizeId == '6'){ //zippo
	?>
    buttonLog('window-have-zippo');
    $('#zippo').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	if($prizeId == '7'){ //jbl
	?>
    buttonLog('window-have-jbl');
    $('#jbl').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	if($prizeId == '8'){ //mouse
	?>
    buttonLog('window-have-mouse');
    $('#mouse').bPopup({
        // positionStyle:'fixed',
        modalClose: false
    });
	<?php
	}
	}
	?>
    console.log(confirmRule);
    /*if(confirmRule=='0'){
        $('#rule-bPopup').bPopup({
            // positionStyle:'fixed',
            modalClose: false
        });
    }*/

    (function(){
        var integral_mall ={
            rule_open: function (){

                $('#rule').on('click',function () {
                    buttonLog('button-index-rule');
                    buttonLog('window-index-rule');

                    $('#rule-bPopup').bPopup({
                        modalClose: false
                    });
                });
                $('#agree-return').on('click',function () {
                    buttonLog('button-index-agree-rule');

                    if($(":checkbox").get(0).checked){
                        if(confirmRule=='0') {
                            $.ajax({
                                url: "http://wanmiclub.lovemojito.com/wx/index.php?s=/Home/IntegralStoreNext/confirmRule",
                                method: 'post',
                                dataType: 'json',
                                data: {'openid': '<?php echo $openid;?>'},
                                success: function (res) {
                                    confirmRule = 1;
                                    $('#rule-bPopup input').hide();
                                    $('#rule-bPopup .ihave').hide();
                                    $('#rule-bPopup #agree-return').hide();

                                },
                                error: function () {
                                    alert('当前网络状况较差，请检查网络后再试!');
                                    return false;
                                }
                            });
                            $("#confirm").attr('disabled', 'true');
                        }
                        $('#rule-bPopup').bPopup().close();

                    }


                })
            },
            init: function (){
                $('.into').on('click',function (e) { //换物品，判断是否同意条款

                    e.preventDefault();

                    var value = $(this).attr('itemid');
                    buttonLog('button-buy-item'+value); //1-4对应四档

                    switch (value){
                        case '1':
                            if(integral>=500){
                                buttonLog('window-integralNotRight');

                                $('#integralNotRight').bPopup();
                                return false;
                            };
                            if(integral<1){
                                buttonLog('window-integralNotEnough');

                                $('#integralNotEnough').bPopup();
                                return false;

                            };
                            break;
                        case  '2':
                            if(integral>=800){
                                buttonLog('window-integralNotRight');
                                $('#integralNotRight').bPopup();
                                return false;

                            };
                            if(integral<500){
                                buttonLog('window-integralNotEnough');
                                $('#integralNotEnough').bPopup();
                                return false;

                            };
                            break;
                        case  '3':
                            if(integral>=5000){
                                buttonLog('window-integralNotRight');

                                $('#integralNotRight').bPopup();
                                return false;

                            };
                            if(integral<800){
                                buttonLog('window-integralNotEnough');

                                $('#integralNotEnough').bPopup();
                                return false;

                            };
                            break;
                        case  '4':
                            if(integral<5000){
                                buttonLog('window-integralNotEnough');

                                $('#integralNotEnough').bPopup();
                                return false;
                            };
                            break;
                        default:
                            alert('网络错误,请刷新页面');
                            return false;
                            break;

                    }
                    buttonLog('window-buyitem'+value);
                    document.cookie = 'value=' + value;
                    window.location.href = 'ExchangePage.php';

                });
            },
            upload: function (){
                this.rule_open();
                this.init();
            }
        };  //认定规则
        integral_mall.upload();
    }());
    function buttonLog(button) {
        $.ajax({
            type:"get",
            url:"http://wanmiclub.lovemojito.com/wx/index.php?s=Home/IntegralStoreNext/integralStoreButton",
            dataType:'JSON',
            async:false,
            data:{'button':button,"openid":openid},
            success:function (data) {
                console.log(data);
            },
            error:function (data) {
                console.log('error');
            }
        })
    }


</script>
<?php
$pvuvLogObj->kmResult = json_encode($member,JSON_UNESCAPED_UNICODE);
$pvuvLogObj->saveLog();
?>
